package com.jiandian.controller;

import com.jiandian.crudparams.dto.AddSignUserDto;
import com.jiandian.crudparams.dto.EditSignUserDto;
import com.jiandian.crudparams.swaggernotes.SignUserNotes;
import com.jiandian.entity.SignUser;
import com.jiandian.page.PageInfo;
import com.jiandian.service.SignUserService;
import com.jiandian.util.BaseController;
import com.jiandian.util.BaseResult;
import com.jiandian.util.BeanUtils;
import com.jiandian.util.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 签到用户
 *
 * @author houqi Fu
 * @date 2018/12/15
 */
@Api(value = "SignUser", description = "签到用户")
@RestController
@RequestMapping(value = Constants.CODE_VERSION + "/SignUser")
public class SignUserController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(SignUserController.class);

    @Autowired
    private SignUserService service;

    @ApiOperation(value = "添加", notes = SignUserNotes.ADD_NOTE)
    @ApiImplicitParam(name = "record", value = SignUserNotes.ADD_VALUE, required = true, dataType = "SignUserVo")
    @PostMapping("/")
    public BaseResult add(@RequestBody @Validated AddSignUserDto record, BindingResult result) {
        logger.info("添加签到用户，参数：\n {}", ReflectionToStringBuilder.toString(record));
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            logger.error("添加签到用户参数错误:{}", fieldError.getDefaultMessage());
            return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
        }
        SignUser bean = new SignUser();
        BeanUtils.copyProperties(record, bean);
        SignUser saveResult = service.add(bean);
        if (saveResult != null) {
            return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, saveResult);
        }
        return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "修改", notes = SignUserNotes.EDIT_NOTE)
    @ApiImplicitParam(name = "record", value = SignUserNotes.EDIT_VALUE, required = true, dataType = "EditSignUserDto")
    @PutMapping("/{id}")
    public BaseResult edit(@PathVariable("id") String id, @RequestBody @Validated EditSignUserDto record, BindingResult result) {
        logger.info("修改签到用户，参数：\n {}", ReflectionToStringBuilder.toString(record));
        if (result.hasErrors()) {
            FieldError fieldError = result.getFieldError();
            logger.error("修改签到用户参数错误:{}", fieldError.getDefaultMessage());
            return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, fieldError.getDefaultMessage());
        }
        SignUser bean = new SignUser();
        BeanUtils.copyProperties(record, bean);
        bean.setId(Integer.valueOf(id));
        SignUser updateResult = service.update(bean);
        if (updateResult != null) {
            return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, updateResult);
        }
        return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "列表", notes = SignUserNotes.LIST_NOTE)
    @ApiImplicitParam(name = "pageInfo", value = SignUserNotes.LIST_VALUE, required = true, dataType = "PageInfo")
    @GetMapping("/")
    public BaseResult list(SignUser whereEntity, PageInfo pageInfo) {
        logger.info("查询签到用户，参数：\n {}", ReflectionToStringBuilder.toString(pageInfo));
        PageInfo pageResult = service.getPage(whereEntity, pageInfo);

        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, pageResult);
    }

    @ApiOperation(value = "详情", notes = "签到用户详情")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable("id") String id) {
        logger.info("查询签到用户，参数：\n id = {}", id);
        if (StringUtils.isBlank(id)) {
            return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "参数uuid不存在");
        }
        SignUser record = service.findById(Integer.valueOf(id));
        if (record != null) {
            return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, record);
        }
        return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "记录不存在");
    }

    @ApiOperation(value = "删除", notes = "根据签到用户id删除")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable("id") String id) {
        logger.info("删除签到用户，参数：\n id = {}", id);
        if (StringUtils.isBlank(id)) {
            return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "参数id不存在");
        }
        int deleteResult = service.delete(Integer.valueOf(id));
        if (deleteResult > 0) {
            return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, "删除成功");
        }
        return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "系统异常");
    }

    @ApiOperation(value = "批量删除", notes = "根据签到用户id list批量删除")
    @PostMapping("batchDelete")
    public BaseResult batchDelete(@RequestBody List<Integer> ids) {
        logger.info("批量删除签到用户，参数：\n {}", ReflectionToStringBuilder.toString(ids, ToStringStyle.MULTI_LINE_STYLE));
        if (CollectionUtils.isEmpty(ids)) {
            return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "参数ids不存在");
        }
        int deleteResult = service.batchDeleteById(ids);
        if (deleteResult > 0) {
            return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, "删除成功");
        }
        return sendResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400, "系统异常");
    }


    @ApiOperation(value = "根据机器编号查询当前上机用户")
    @GetMapping("/selectByMachineNumber")
    public BaseResult selectByMachineNumber(@RequestParam String machineNumber) {

        SignUser signUser = service.getAndInsertByMachineNumber(machineNumber);
        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, signUser);
    }

    @ApiOperation(value = "根据用户id签到")
    @GetMapping("/signin")
    public BaseResult signinById(@RequestParam Integer id,@RequestParam String machineNumber) {

        String result = service.signinByIdAndMachineNumber(id,machineNumber);

        return sendResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, result);
    }


}
